package com.metagurukul.informer.servlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.metagurukul.informer.service.ForgotPasswordService;
import com.metagurukul.informer.utility.ExceptionProperties;
import com.metagurukul.informer.utility.SendMail;
import com.metagurukul.informer.utility.Validator;

/**
 * Servlet implementation class ForgotPassword
 */
@WebServlet("/ForgotPassword")
public class ForgotPassword extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ForgotPassword() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * if user is a member than it send the password to user account
     * else send message to register first
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void doPerform(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	
    	Validator validator = new Validator();
		Map<String, String> exceptionmap = new HashMap<String, String>();
    	String emailId=request.getParameter("email");
    	//server side validation
    	if (emailId == null) {
			exceptionmap.put("email", ExceptionProperties._101);
		} else {
			if (emailId == "") {
				exceptionmap.put("email", ExceptionProperties._106);
			} else {
				if (! validator.emailValidate(emailId)) {
					exceptionmap.put("email", ExceptionProperties._102);
				}
			}
		}
    	
    	//if there is no validation failure
    	if(exceptionmap.size()==0){
    		emailId=emailId.trim();
    		ForgotPasswordService forgotPasswordService=new ForgotPasswordService(emailId);
    		try {
    			if(forgotPasswordService.isUserExist()){
    				String password=forgotPasswordService.getPassword();
    				SendMail sendmail = new SendMail(); 
					sendmail.sendMail(emailId, "Forgot Password","your password is : "+password);
					response.sendRedirect("login.jsp");
        		}else{
        			exceptionmap.put("email", ExceptionProperties._111);
        			request.setAttribute("exceptionmap", exceptionmap);
            		request.getRequestDispatcher("ForgotPassword.jsp").forward(request, response);
        		}
			} catch (Exception e) {
				// TODO: handle exception
			}
    		
    		
    	}else{
    		request.setAttribute("exceptionmap", exceptionmap);
    		request.getRequestDispatcher("ForgotPassword.jsp").forward(request, response);
    	}
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPerform(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPerform(request,response);
	}

}
